<!DOCTYPE html>
<html>

<head>
    <meta charset=utf-8>
    <meta name=viewport content="initial-scale=1,maximum-scale=1,user-scalable=no">
    <meta name=format-detection content="telephone=no">

    <title>PDF阅读器</title>
    <style>
        .header {
            position: fixed;
            left: 0;
            top: 0;
            right: 0;
            color: #FFF;
            font-size: 14px;
            padding-left: 12px;
            overflow: auto;
        }

        .header>img {
            display: inline-block;
            vertical-align: middle;
        }

        .header>.close,.header>.download {
            display: inline-block;
            color: #FFF;
            vertical-align: middle;
            font-size: 16px
        }
        .download{
          float: right;
          margin-right: 12px;
          display: none;
        }

    </style>
</head>

<body>
    <div class="header" id='head'>
        <img src="./static/back.png" height="24px" alt="" onclick="page.backLastPage()">
        <div class="close" onclick="page.backLastPage()">关闭</div>
        <div class="download" onclick="page.downloadPage()" id="download">下载</div>
    </div>
</body>
<script>
    var page = {
        backLastPage: function () { }
    }
    var pdf = '', dark = false,download = false

    apiready = function () {
        var reader = api.require('pdfReader')
        pdf = api.pageParam.pdf
        dark = api.pageParam.dark
        download = api.pageParam.download
        api.setStatusBarStyle({style:'light',color:'rgba(0,0,0,0)',animated:false})
        document.getElementById('head').style.paddingTop = (api.safeArea.top + 8) + 'px'
        if(download) document.getElementById('download').style.display = 'inline-block';
        reader.openView({
            rect: {
                x: 0,
                y: api.safeArea.top + 50,
                w: api.winWidth,
                h: api.winHeight - api.safeArea.top - 50
            },
            path: pdf,
            fixedOn: 'pdf',
            fixed: false,
            showLoading: false
        })
        page.backLastPage = function () {
            api.animation({
                name: 'pdf',
                delay: 0,
                duration: 200,
                curve: 'ease_in_out',
                repeatCount: 0,
                autoreverse: false,
                alpha: 1,
                translation: {
                    x: 0,
                    y: api.winHeight,
                    z: 0
                },
            }, function (ret, err) {
                if (ret) {

                    api.execScript({
                        frameName: 'app',
                        script: 'page.reshow&&page.reshow()'
                    })

                    reader.closeView()
                    api.closeFrame({
                        name: 'pdf'
                    })
                }
            })
        }
        page.downloadPage = function(){
          api.requestPermission({
            list: ['storage']
          }, function (ret) {
            if (ret.list && ret.list.filter(x => x.name == 'storage' && x.granted).length) {
              api.download({
                url: pdf,
                savePath: 'fs://download/发票.pdf',
                report: true,
                cache: true,
                allowResume: true
              }, function(ret, err) {
                if (ret.state == 1) {
                  //下载成功
                  api.toast({
                      msg: '下载成功，存储路径：'+ret.savePath,
                      duration: 2000,
                      location: 'bottom'
                  });
                } else {
                  api.toast({
                      msg: '下载失败',
                      duration: 2000,
                      location: 'bottom'
                  });
                }
              });
            } else {}
          })
        }
    }
</script>

</html>
